package com.todo.biz.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.biz.application.convert.BizBaseConvert;
import com.todo.biz.application.entity.bo.FeedbackBO;
import com.todo.biz.application.entity.bo.FeedbackDetailBO;
import com.todo.biz.application.entity.condition.FeedbackConditionBO;
import com.todo.biz.application.frontend.service.FeedbackAggService;
import com.todo.biz.application.service.FeedbackService;
import com.todo.biz.iface.frontend.request.*;
import com.todo.biz.iface.frontend.response.FeedbackBaseVO;
import com.todo.biz.iface.frontend.response.FeedbackDetailVO;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (Feedback)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:46
 */
@Slf4j
@Service("feedbackAggService")
public class FeedbackAggServiceImpl implements FeedbackAggService {

    @Resource
    private FeedbackService feedbackService;

    @Override
    public BizResponse<Boolean> insertFeedback(InsertFeedbackParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        FeedbackBO feedbackBO = BizBaseConvert.convertToFeedbackBO(param);
        Boolean insertResult = feedbackService.insertFeedback(feedbackBO);
        return BizResponse.success(insertResult);
    }

    @Override
    public BizResponse<Boolean> removeFeedback(RemoveFeedbackParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<Long> collect = param.getIdList().stream().map(Long::valueOf).collect(Collectors.toList());
        Boolean removeResult = feedbackService.removeFeedbackByIdList(collect);
        return BizResponse.success(removeResult);
    }

    @Override
    public BizResponse<Boolean> updateFeedback(UpdateFeedbackParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        FeedbackBO feedbackBO = BizBaseConvert.convertToFeedbackBO(param);
        Boolean updateResult = feedbackService.updateFeedback(feedbackBO);
        return BizResponse.success(updateResult);
    }

    @Override
    public BizResponse<FeedbackDetailVO> queryFeedbackById(QueryFeedbackByIdParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        FeedbackDetailBO feedbackDetailBO = feedbackService.queryFeedbackById(Long.valueOf(param.getFeedbackId()));
        if (feedbackDetailBO == null) {
            log.info("FeedbackAggService#queryFeedbackById 根据id查询反馈信息完成 数据为空 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.success();
        }
        return BizResponse.success(BizBaseConvert.convertToFeedbackDetailVO(feedbackDetailBO));
    }

    @Override
    public BizPageResponse<FeedbackBaseVO> queryFeedbackByUserId(QueryFeedbackByUserIdParam param) {
        if (param == null || !param.baseCheck()) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        FeedbackConditionBO feedbackConditionBO = new FeedbackConditionBO();
        feedbackConditionBO.setCreateUser(param.get_user().getUserId());
        feedbackConditionBO.setPageIndex(param.getPageIndex());
        feedbackConditionBO.setPageSize(param.getPageSize());
        feedbackConditionBO.setPageIndex(param.getPageIndex());
        PageResult<FeedbackBO> feedbackResponsePageResult = feedbackService.queryFeedbackByCondition(feedbackConditionBO);
        if (feedbackResponsePageResult == null) {
            log.info("FeedbackAggService#queryFeedbackByUserId 根据用户分页查询反馈信息完成 数据为空 入参：【{}】", JSONObject.toJSONString(param));
            return BizPageResponse.success();
        }
        return BizPageResponse.success(BizBaseConvert.convertToFeedbackBaseVOPageResult(feedbackResponsePageResult));
    }
}
